﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PurchaseManagementSystem.DBModel;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

namespace PurchaseManagementSystem
{
    public class PurchasePart
    {
        private int orderid { get; set; }
        private int purchase_employee_id { get; set; }
        private int productid { get; set; }
        //int product_id;

        public PurchasePart(int oid, int p_e_id, int pid)    //数据库传入的初始订单数据
        {
            this.orderid = oid;
            this.purchase_employee_id = p_e_id;
            this.productid = pid;
            
        }

        static string sql = string.Empty;

        public void GetAllOrder()
        {
            /*
             *获得初始订单
             * 将订单号传递给仓库
             * 有仓库进行下一步订单判断
             */
            //DataTable ini_dt = DBServer.GetOrderByOid_Pid(orderid, productid);  //获得初始订单

            /*下面功能为：
            *接受仓库的返回数据（需采购和不需采购产品）
            *生成最终采购订单
            */

       

            DataTable dt = DBServer.GetPassedOrderByOid_Pid(orderid,productid);// 获得通过商品列表
            
            foreach (DataRow row in dt.Rows)                //遍历通过商品列表
            {
                db_passed_products_list order = new db_passed_products_list();   // 根据通过商品列表生成最终订单
                
                order.Record_id = (int)row["Record_id"];     //物品IDg
                order.Employee_id = (int)row["Employee_id"];
                order.Product_id = (int)row["Product_id"];
                order.Time = (DateTime)row["Time"];
                order.Product_num = (int)row["Product_num"];

                DataTable dt1 = DBServer.GetPassedProductByProductId(order.Product_id);
                foreach(DataRow row1 in dt1.Rows)
                {
                    db_product_information p_i = new db_product_information();
                    p_i.Product_price = (int)row1["Product_price"];
                    int tot_price = p_i.Product_price * order.Product_num;

                    if (order != null)
                    {
                        if (tot_price <= 50000)
                        {
                            DBServer.Insertinto_final_order(order.Record_id, order.Employee_id, order.Product_id, order.Time, order.Product_num,tot_price);

                        }
                        else
                        {   

                            DBServer.Insertinto_nopass(order.Record_id, order.Employee_id, order.Time, order.Product_id, order.Product_num,"TOO_MUCH");
                            DBServer.Deletefrom_passed_products_list(order.Product_id, order.Record_id);
                        }
                    }
                    else
                    {
                    MessageBox.Show("操作失败！！！");
                    }
                }
            }
        


        }


        /*
                public int getOrderId()     //供仓库获得orderid
                {
                    return this.orderid;
                }

                public int getEmployeeId()
                {
                    return this.purchase_employee_id;
                }
    
 
        */

    }
}
